{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十一题 斐波那切数列",
   "id": "ffaa85fcb3a48550"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 又称兔子数列，值得是这样的一个数列：1、1、2、3、5、8、13、21、34、......编写程序找出第n项",
   "id": "3656d57451b3ea81"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-03T07:17:44.546014Z",
     "start_time": "2025-03-03T07:17:44.427140Z"
    }
   },
   "source": [
    "# def tuzi(n):\n",
    "#     num1=1\n",
    "#     num2=1\n",
    "#     num2=num1+num2\n",
    "#     return num2\n",
    "# \n",
    "# print(tuzi(n=n))"
   ],
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'n' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[2], line 7\u001B[0m\n\u001B[0;32m      4\u001B[0m     num2\u001B[38;5;241m=\u001B[39mnum1\u001B[38;5;241m+\u001B[39mnum2\n\u001B[0;32m      5\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m num2\n\u001B[1;32m----> 7\u001B[0m \u001B[38;5;28mprint\u001B[39m(tuzi(n\u001B[38;5;241m=\u001B[39mn))\n",
      "\u001B[1;31mNameError\u001B[0m: name 'n' is not defined"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:20:44.692021Z",
     "start_time": "2025-03-03T07:20:44.684795Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#递归\n",
    "def fib(n):\n",
    "    if n==1 or n==2:\n",
    "        return 1\n",
    "    else:\n",
    "        return fib(n-1)+fib(n-2)\n",
    "\n",
    "print(fib(3),fib(4),fib(5))"
   ],
   "id": "c1d840b85a76ebaa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 5\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:29:59.685351Z",
     "start_time": "2025-03-03T07:29:59.666991Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#非递归\n",
    "n=6\n",
    "list=[1,1]\n",
    "for i in range(2,n+1):\n",
    "    list.append(list[i-1]+list[i-2])\n",
    "print(list[n-1])"
   ],
   "id": "4ba0b84c707edded",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十二题 复制列表",
   "id": "81b5d788edc8638b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 将一个列表的数据复制到另一个列表中",
   "id": "a263488e1bfd17eb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:34:31.427697Z",
     "start_time": "2025-03-03T07:34:31.415821Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1=[1,2,3,4,5]\n",
    "list2=[]\n",
    "for i in list1:\n",
    "    list2.append(i)\n",
    "list1[0]=0   \n",
    "print(list2)"
   ],
   "id": "15c10d9f803ba669",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:35:26.981332Z",
     "start_time": "2025-03-03T07:35:26.963804Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import copy\n",
    "list=[1,2,3,4,5]\n",
    "list1=copy.copy(list)\n",
    "list[0]=0\n",
    "print(list1)"
   ],
   "id": "21e89f90b45226ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十三题 暂停后输出",
   "id": "5c7441209f604e30"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 暂停一段时间后输出",
   "id": "ff249fec302d7d47"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:41:54.987503Z",
     "start_time": "2025-03-03T07:41:50.973618Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import time\n",
    "time.sleep(4)\n",
    "print('Hello Python')"
   ],
   "id": "732bb85c7d988c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Python\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十四题 成绩if语句",
   "id": "9125cbf91095894d"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 利用条件运算符的嵌套来完成：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。",
   "id": "cfc29ebd67038116"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:51:02.459628Z",
     "start_time": "2025-03-03T07:50:59.462780Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score=int(input(\"请输入成绩：\"))\n",
    "s=''\n",
    "if score>=90:\n",
    "    s='A'    #可以直接print('A')\n",
    "elif score>=60 and score<90:\n",
    "    s='B'\n",
    "elif score>=0 and score<60:\n",
    "    s='C'\n",
    "else:\n",
    "    print('请重新输入')\n",
    "print(f'您的成绩为：{s}')\n",
    "    "
   ],
   "id": "6645cba6152e7340",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您的成绩为：B\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十五题 统计字符",
   "id": "5c78927ddf7bfed9"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数",
   "id": "ff5afbd05539a18b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:08:45.627090Z",
     "start_time": "2025-03-03T08:08:41.378421Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list=str(input('请输入一行字符'))\n",
    "count_A=0\n",
    "count_1=0\n",
    "count=0\n",
    "count_e=0\n",
    "for i in list:\n",
    "    #判断空格\n",
    "    if i==' ':   #if i.isspace():\n",
    "        count+=1\n",
    "    \n",
    "    #判断数字\n",
    "    elif ord(i)>=48 and ord(i)<=57:  #if isdigit():\n",
    "        count_1+=1\n",
    "    \n",
    "    #判断字母\n",
    "    elif (ord(i)>=65 and ord(i)<=90) or (ord(i)>=97 and ord(i)<=122):   #if i.isalpha():\n",
    "        count_A+=1\n",
    "    \n",
    "    #判断其他\n",
    "    else:\n",
    "        count_e+=1\n",
    "print(list)\n",
    "print(f'英文字母个数为{count_A}、空格个数为{count}、数字个数为{count_1},其它字符的个数为{count_e}')#Liu昱晓 20040624 &"
   ],
   "id": "14ee8ffaf809dcee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Liu昱晓 20040624 &\n",
      "英文字母个数为3、空格个数为2、数字个数为8,其它字符的个数为3\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#",
   "id": "bf580130204ae687"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:16:55.974496Z",
     "start_time": "2025-03-03T08:16:53.759500Z"
    }
   },
   "cell_type": "code",
   "source": [
    "str=input('请输入一行字符')\n",
    "num=0\n",
    "space=0\n",
    "char=0\n",
    "other=0\n",
    "for i in str:\n",
    "    if i.isalpha(): #判断字母，有点弊端，会把别的语言“误认”\n",
    "        char+=1\n",
    "    elif i.isdigit(): #判断数字\n",
    "        num+=1\n",
    "    elif i.isspace(): #判断空格\n",
    "        space+=1\n",
    "    else:\n",
    "        other+=1\n",
    "print(str)\n",
    "print(f'英文字母个数为{char}、空格个数为{space}、数字个数为{num},其它字符的个数为{other}')#Liu昱晓 20040624 &"
   ],
   "id": "ae5230b01f158a7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Liu昱晓 20040624 &\n",
      "英文字母个数为5、空格个数为2、数字个数为8,其它字符的个数为1\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十五题 落体反弹问题",
   "id": "166b664987aa22d"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 一球从100米高度自由落下，每次落地后反跳回原高度的一半；再落下，求它在第十次落地时，共经过多少米？第10次反弹多高？",
   "id": "c48e8894ace3d692"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:29:24.057789Z",
     "start_time": "2025-03-03T08:29:24.033203Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# height=100\n",
    "# sum=0\n",
    "# for i in range(1,11):\n",
    "#     sum+=height\n",
    "#     height=height/2\n",
    "#     sum+=height\n",
    "# print(f'它在第十次落地时，共经过{sum-height}米，第10次反弹{height}米')\n",
    "#emmmmmm..."
   ],
   "id": "aca7f6cdccf33ab6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "它在第十次落地时，共经过299.609375米，第10次反弹0.09765625米\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:32:29.172162Z",
     "start_time": "2025-03-03T08:32:29.156816Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#emmmmmm......\n",
    "n=100\n",
    "count=0\n",
    "list=[]\n",
    "\n",
    "while count<10:\n",
    "    if count==0:\n",
    "        list.append(n)\n",
    "        n/=2\n",
    "        count+=1\n",
    "    else:\n",
    "        list.append(2*n)\n",
    "        n/=2\n",
    "        count+=1\n",
    "print(list)"
   ],
   "id": "e07de2a6dc5f7a0b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100, 100.0, 50.0, 25.0, 12.5, 6.25, 3.125, 1.5625, 0.78125, 0.390625]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十七题 猴子吃桃问题",
   "id": "ff5ac5e1dca86067"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 猴子吃桃问题：猴子第一天摘下若干个桃子，当即吃了一半，还不过瘾，又多吃了一个；第二天早上又将剩下的桃子吃掉一半，又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第十天早上想再吃时，见只剩下一个桃子了。求第一天共摘了多少。",
   "id": "339692a9e0cd3d1d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:55:30.648423Z",
     "start_time": "2025-03-03T08:55:30.630877Z"
    }
   },
   "cell_type": "code",
   "source": [
    "i=1\n",
    "total=1\n",
    "while i<10:\n",
    "   total=(total+1)*2\n",
    "   i+=1\n",
    "\n",
    "print(f'第一天共摘了{total}个桃')"
   ],
   "id": "773bf1c5d6c3d64a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一天共摘了1534个桃\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:56:39.167362Z",
     "start_time": "2025-03-03T08:56:39.154582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "n=1\n",
    "for i in range(1,10):\n",
    "    total=(n+1)*2\n",
    "    n=total\n",
    "print(total)"
   ],
   "id": "d58012320eddafb8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1534\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十八题 求指定数列的和",
   "id": "24f9869b4a65a1cd"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 有一份数列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前20项之和",
   "id": "5956767a54ce77e1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T09:14:28.966519Z",
     "start_time": "2025-03-03T09:14:28.958708Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#搞错了搞错了，分母是前一项分子\n",
    "#j=1\n",
    "# sum=0\n",
    "# num=1\n",
    "# num2=2\n",
    "# for i in range(1,21): #分母\n",
    "#     j+=num  \n",
    "#     sum+=j/num2\n",
    "#     num1=i\n",
    "#     num2=j\n",
    "# print(sum)"
   ],
   "id": "47480b08ed4e672c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22.597739657143684\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T09:18:38.347177Z",
     "start_time": "2025-03-03T09:18:38.337051Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sum=0\n",
    "up=2\n",
    "down=1\n",
    "\n",
    "for i in range(20):\n",
    "    sum+=up/down\n",
    "    # up,down=up+down,up\n",
    "    a=down\n",
    "    down=up\n",
    "    up=up+a\n",
    "print(sum)"
   ],
   "id": "c09bb0a4326e224f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32.66026079864164\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第二十九题 求各个阶乘的和",
   "id": "39bac8e1a1dca47b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 求1！+2！+3！+...+20！",
   "id": "3bb19ac6792da1ec"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T09:35:28.834986Z",
     "start_time": "2025-03-03T09:35:28.826319Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sum=0\n",
    "\n",
    "def jiecheng(num):\n",
    "    ji=1\n",
    "    for i in range(1,num+1):\n",
    "        ji=ji*i\n",
    "    return ji\n",
    "\n",
    "for j in range(1,21):\n",
    "    sum+=jiecheng(j)\n",
    "print(sum)\n",
    "    \n",
    "        "
   ],
   "id": "457173da97d0b8d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "2561327494111820313\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import math\n",
    "math.factorial()#返回阶乘"
   ],
   "id": "7d380276437a8cb9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T09:41:17.745025Z",
     "start_time": "2025-03-03T09:41:17.727217Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import math \n",
    "sum=0\n",
    "for i in range(1,21):\n",
    "    sum+=math.factorial(i)\n",
    "print(sum)"
   ],
   "id": "dd490e6f0372e33",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2561327494111820313\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 第三十题 年龄急转弯",
   "id": "69eba2753dd96871"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 有5个人坐在一起，问第五个人多少岁？他说比第四个大2岁。问第4个人岁数，他说比第三个人大2岁。问第三个人，又说比第2个人大两岁。问第二个人，说比第一个人大两岁。最后问第一个人，他说是10岁。请问第五个人多大？",
   "id": "8046052ae5b918b1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T10:40:38.265693Z",
     "start_time": "2025-03-03T10:40:38.231367Z"
    }
   },
   "cell_type": "code",
   "source": [
    "age1=10\n",
    "age2=age1+2\n",
    "age3=age2+2\n",
    "age4=age3+2\n",
    "age5=age4+2\n",
    "print(age5)"
   ],
   "id": "ce0b59dc23c2c626",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T10:43:03.433736Z",
     "start_time": "2025-03-03T10:43:03.422568Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#递归\n",
    "def func(n):\n",
    "    if n==1:\n",
    "        return 10\n",
    "    else:\n",
    "        return func(n-1)+2\n",
    "print(func(5))"
   ],
   "id": "bcefdb9491a76a1e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "execution_count": 46
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
